#include "login.h"
#include "register.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Widget);//使dialog项目有最大最小化按钮
    setWindowTitle("登录");
}

Login::~Login()
{
    delete ui;
}

void Login::on_register_2_clicked()
{
    this->a.show();//非模态窗口，在login.h里的Login类里写入Register a;
}

void Login::on_login_clicked()
{
    QString userName=this->ui->userName->text();
    QString password=this->ui->password->text();
    QSqlQuery query;

    if (userName.isEmpty()||password.isEmpty())
    {
        QMessageBox::information(NULL, "提示", "账号密码不得为空！", QMessageBox::Ok);
    }
    else
    {
        query.exec("select * from user where 账号 ='"+userName+"'");//最左边两个双引号是一对，然后加号加一个变量，再一个加号，再一对双引号括起来的字符串
        if(!query.isActive())
        {
            QMessageBox::critical(NULL,"错误","查询未执行！", QMessageBox::Ok);
        }
        QString str1,str2;
        while(query.next())
        {
            str1 = query.value("账号").toString();
            str2 = query.value("密码").toString();
            //str3 = query.value("身份").toString();
        }
        if(str1.isEmpty())
        {
            QMessageBox::information(NULL, "提示", "没有该账号！", QMessageBox::Ok);
            this->ui->userName->clear();
            this->ui->password->clear();
            this->ui->userName->setFocus();
        }
        else if(str2!=password)
        {
            QMessageBox::information(NULL, "提示", "账号存在，密码错误！", QMessageBox::Ok);
            this->ui->password->clear();
            this->ui->userName->setFocus();
        }
        else
        {
            QMessageBox::information(NULL, "提示", "登录成功！", QMessageBox::Ok);
            this->close();
            this->b.show();
        }
    }
}
